package com.yuan.department.controller;

import com.alibaba.fastjson.JSON;
import com.yuan.department.constants.BaseConstants;
import com.yuan.department.entity.Emp;
import com.yuan.department.service.IEmpService;
import com.yuan.department.service.impl.EmpServiceImpl;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: Train
 * @description:
 * @author: yuan_boss
 * @create: 2022-11-23 18:43
 **/

@WebServlet("/EmpServlet/*")
public class EmpServlet extends BaseServlet {
    private IEmpService service = new EmpServiceImpl();

    public String add(HttpServletRequest request, HttpServletResponse response) throws InvocationTargetException, IllegalAccessException, SQLException {
        System.out.println("EmpServlet.add");
        //获取表单数据并且放到map中
        Map<String, String[]> map = request.getParameterMap();
        //拷贝表单数据到实体类中
        Emp emp = new Emp();
        System.out.println(map);
        BeanUtils.populate(emp,map);
        System.out.println("拷贝后的emp: " + emp);

        //入库
        boolean save = service.save(emp);
//        if (!save){
//            request.setAttribute("msg","添加失败");
//        }else {
//            request.setAttribute("msg","添加成功");
//        }
//        //重定向
//        return BaseConstants.BASE_FORWARD + ":EmpServlet/list";
        return outRedirect(save,"EmpServlet/list");
    }
    public String addEmpAsync(HttpServletRequest request, HttpServletResponse response) throws IOException, SQLException {
        String text = request.getReader().readLine();
        Emp emp = JSON.parseObject(text, Emp.class);
        boolean result = false;
//        int a = 5/0;
        try {
            result = service.save(emp);
        } catch (SQLException e) {
            return outResult(false);
        }
        return outResult(result);

    }
    public String updateEmpAsync(HttpServletRequest request, HttpServletResponse response) throws IOException{
        String text = request.getReader().readLine();
        Emp emp = JSON.parseObject(text, Emp.class);
        boolean result = false;
        try {
            result = service.update(emp);
        } catch (SQLException e) {
            return outResult(result);
        }
        return outResult(result);
    }

    public String update(HttpServletRequest request, HttpServletResponse response) throws InvocationTargetException, IllegalAccessException, SQLException {
        System.out.println("EmpServlet.update");
        //获取表单数据并且放到map中
        Map<String, String[]> map = request.getParameterMap();
        //拷贝表单数据到实体类中
        Emp emp = new Emp();
        BeanUtils.populate(emp,map);
        System.out.println("即将更新为: " + emp);
        boolean update = service.update(emp);
//        if (!update){
//           request.setAttribute("msg","更新失败");
//        }else {
//            request.setAttribute("msg","更新成功");
//        }
//
//        return BaseConstants.BASE_FORWARD + ":EmpServlet/list";
        return outRedirect(update,"EmpServlet/list");
    }

    public String delete(HttpServletRequest request, HttpServletResponse response) throws SQLException {
        System.out.println("EmpServlet.delete");
        String empno = request.getParameter("empno");
        int i = Integer.parseInt(empno);
        boolean remove = service.remove(i);
//        if (!remove){
//            request.setAttribute("msg","删除失败");
//        }else {
//            request.setAttribute("msg","删除成功");
//        }
//        return BaseConstants.BASE_FORWARD + ":EmpServlet/list";
        return outRedirect(remove,"EmpServlet/list");
    }

    public String list(HttpServletRequest request, HttpServletResponse response) throws SQLException {
        System.out.println("EmpServlet.list");
        List<Emp> list = service.list();
        request.setAttribute("empList",list);
        return BaseConstants.BASE_FORWARD + ":emp/empList.jsp";
    }

    public String selectOne(HttpServletRequest request, HttpServletResponse response) throws SQLException {
        System.out.println("EmpServlet.selectOne");
        String empno = request.getParameter("empno");
        Emp emp = service.queryById(Integer.parseInt(empno));
        System.out.println("查找到一个emp: " + emp);
        if (emp!=null){
            request.setAttribute("empOne",emp);
        }
        return BaseConstants.BASE_FORWARD + ":/emp/updateEmp.jsp";
    }

    public void listJobs(HttpServletRequest request, HttpServletResponse response) throws IOException {
        System.out.println("异步获取职位信息");
        Map<String,Object> map = new HashMap<>();
        List<String> jobs = new ArrayList<>();
        jobs.add("前端工程师");
        jobs.add("后端工程师");
        map.put("jobs",jobs);
        String s = JSON.toJSONString(map);
        response.getWriter().println(s);
    }

}
